﻿Imports MbUnit.Framework

Imports FM.NetBouncer.Rules
Imports FM.NetBouncer

<TestFixture()> _
Public Class RulesFixture


    <RowTest()> _
        <Row("1", "", True)> _
        <Row("9946", "", True)> _
        <Row("99999999999999999999998798797979879999999999999999", "", False)> _
        <Row("asd", "", False)> _
        <Row(Nothing, "", False)> _
        <Row("987952x2x2", "", False)> _
    Public Sub IsInteger(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Rule As New IsInteger()

        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
        Assert.AreEqual(Rules.IsInteger.Valid(input), expectedType, desc)

    End Sub


    <RowTest()> _
        <Row("1", "", True)> _
        <Row("9946", "", True)> _
        <Row("99999999999979879999999999999999", "", False)> _
        <Row("xx1x1", "", False)> _
        <Row(Nothing, "", False)> _
        <Row("987952x2x2", "", False)> _
        <Row("0", "", False)> _
        <Row("-987952x2x2", "", False)> _
        <Row("-9", "", False)> _
    Public Sub IsPositiveInteger(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Rule As New IsPositiveInteger()

        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
        Assert.AreEqual(Rules.IsPositiveInteger.Valid(input), expectedType, desc)


    End Sub

    <RowTest()> _
     <Row("-1", "", True)> _
     <Row("-9946", "", True)> _
     <Row("-99999999999979879999999999999999", "", False)> _
     <Row("9999999999979879999999999999999", "", False)> _
     <Row("xx1x1", "", False)> _
     <Row(Nothing, "", False)> _
     <Row("987952x2x2", "", False)> _
     <Row("0", "", False)> _
     <Row("9", "", False)> _
     <Row("1", "", False)> _
     Public Sub IsNegativeInteger(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Rule As New IsNegativeInteger()
        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
        Assert.AreEqual(Rules.IsNegativeInteger.Valid(input), expectedType, desc)
    End Sub

    <Test()> _
    Public Sub IsInRange()

        Dim Rule As New IsInRange(10, 30)

        Assert.IsTrue(Rule.IsValid("20"))
        Assert.IsTrue(Rule.IsValid("10"))
        Assert.IsTrue(Rule.IsValid("30"))

        Assert.IsFalse(Rule.IsValid("9"))
        Assert.IsFalse(Rule.IsValid("31"))

        Assert.IsFalse(Rule.IsValid("8798798798"))
        Assert.IsFalse(Rule.IsValid("asdasd"))
        Assert.IsFalse(Rule.IsValid("99999999999999999999998798797979879999999999999999"))
        Assert.IsFalse(Rule.IsValid("xx1x1"))
        Assert.IsFalse(Rule.IsValid("987952x2x2"))

    End Sub


    <RowTest()> _
   <Row("test", "", True)> _
   <Row("TEST2", "", True)> _
   <Row("TEST", "Case Sensitivity", False)> _
   <Row("test ", "Trim", False)> _
   <Row(Nothing, "Trim", False)> _
   <Row("9", "", False)> _
   <Row("kdas;dojas;odjas;dolja!", "", False)> _
    Public Sub WhiteListing(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim List() As String = {"test", "TEST2"}

        Dim Rule As New WhiteListItems(List)
        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
        Assert.AreEqual(WhiteListItems.Valid(input, List), expectedType, desc)
    End Sub


    <Test()> _
    Public Sub InitTests()
        Dim RuleTest As New IsDataInRange()
        Dim RuleTest2 As New IsNegativeInteger()
        Dim RuleTest3 As New WhiteListCharacters()
        Dim RuleTest4 As New IsNotNull()
        Dim RuleTest5 As New WhiteListItems()

    End Sub

    <Test()> _
    Public Sub LengthMaxAndMin()

        Dim Rule As New IsDataInRange(1, 10)
        Assert.IsTrue(Rule.IsValid("1234567890"))
        Assert.IsTrue(Rule.IsValid("a"))
        Assert.IsTrue(Rule.IsValid("asdasdd"))


        Assert.IsFalse(Rule.IsValid(""))
        Assert.IsFalse(Rule.IsValid(Nothing))
        Assert.IsFalse(Rule.IsValid("012345678901"))
        Assert.IsFalse(Rule.IsValid("0123456789013l;asdfk;lsdkls;dkf;lskf;sldfk"))

    End Sub

    <RowTest()> _
    <Row("1234567890", "", True)> _
    <Row("a", "", True)> _
    <Row("asasdd", "", True)> _
    <Row("", "", False)> _
    <Row(Nothing, "", False)> _
    <Row("012345678901", "", False)> _
    <Row("0123456789013789013l;asdfk;lsdkls;dkf;lskf;sldfl", "", False)> _
    Public Sub DataRangeShortCut(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Assert.AreEqual(IsDataInRange.Valid(input, 1, 10), expectedType)
    End Sub

    <RowTest()> _
     <Row("1234567890", "", True)> _
     <Row("a", "", True)> _
     <Row("asasdd", "", True)> _
     <Row("", "", True)> _
     <Row(Nothing, "", True)> _
     <Row("012345678901", "", False)> _
     <Row("0123456789013789013l;asdfk;lsdkls;dkf;lskf;sldfl", "", False)> _
     Public Sub DataRangeValid2(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Assert.AreEqual(IsDataInRange.Valid(input, 10), expectedType)
    End Sub

    <RowTest()> _
     <Row("1234", "", True)> _
     <Row("", "", True)> _
     <Row("12345", "", True)> _
     <Row("01234567891", "", False)> _
     <Row("010123456789013;sldfl", "", False)> _
    Public Sub LengthMax(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)

        Dim Rule As New IsDataInRange(5)
        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
    End Sub


    <Test()> _
    Public Sub IsNotNothing()

        Dim Rule As New IsNotNull()
        Assert.IsTrue(Rule.IsValid("1234"))
        Assert.IsFalse(Rule.IsValid(Nothing))

        'Direct
        Assert.IsTrue(Rules.IsNotNull.Valid("1234"))
        Assert.IsFalse(Rules.IsNotNull.Valid(Nothing))

    End Sub

    <Test()> _
    Public Sub WhiteListingBasedOnChars()
        Dim Rule As New WhiteListItems()
        Rule.AddList(New String() {"abcdef", "xasd"})

        Assert.IsTrue(Rule.IsValid("abcdef"))
        Assert.IsTrue(Rule.IsValid("xasd"))

        Assert.IsFalse(Rule.IsValid("Abcdef"))
        Assert.IsFalse(Rule.IsValid("ABCDEF"))
        Assert.IsFalse(Rule.IsValid("ab"))
        Assert.IsFalse(Rule.IsValid(""))
        Assert.IsFalse(Rule.IsValid(Nothing))
        Assert.IsFalse(Rule.IsValid("fx"))
        Assert.IsFalse(Rule.IsValid("asdasdasd"))

    End Sub

    <RowTest()> _
    <Row("016546546541651321", "", True)> _
    <Row("0", "", True)> _
    <Row(Nothing, "Nothing", False)> _
    <Row("asd", "", False)> _
    <Row("z", "", False)> _
    Public Sub PatternRule(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Rule As New WhiteListCharacters()
        Rule.AddListFromString(Patterns.Numerical)

        Assert.AreEqual(Rule.IsValid(input), expectedType, desc)
    End Sub

    <RowTest()> _
    <Row("0", "", True)> _
    <Row("065", "", True)> _
    <Row(Nothing, "", False)> _
    <Row("asdad", "", False)> _
    Public Sub PatternShortcut(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        'By default
        Assert.AreEqual(WhiteListCharacters.Valid(input, Patterns.Numerical), expectedType, desc)
    End Sub

    <RowTest()> _
    <Row("aBc", "", True)> _
    <Row(Nothing, "", False)> _
    <Row("0545754a", "", False)> _
    <Row("!asdfs", "", False)> _
    Public Sub PatternShortcutAlpha(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        'By default
        Assert.AreEqual(WhiteListCharacters.Valid(input, Patterns.EnglishAlphaMixedCase), expectedType, desc)
    End Sub

    <RowTest()> _
        <Row("0", "", True)> _
        <Row("065", "", True)> _
        <Row(Nothing, "", False)> _
        <Row("asd", "", False)> _
        <Row("d", "", False)> _
    Public Sub PatternShortcutFromArray(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim NumericPattern As IList(Of Char) = Patterns.Numerical.ToCharArray()
        Assert.AreEqual(WhiteListCharacters.Valid(input, NumericPattern), expectedType, desc)
    End Sub


    <RowTest()> _
    <Row("0", "", True)> _
    <Row("065", "", True)> _
    <Row(Nothing, "", False)> _
    <Row("asd", "", False)> _
    <Row("d", "", False)> _
    Public Sub PatternFromString(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Pat As New WhiteListCharacters()
        Pat.AddList(New String() {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"})
        Assert.AreEqual(Pat.IsValid(input), expectedType, desc)
    End Sub

    <Test()> _
    Public Sub PatternAddListFromString()
        Dim Pat As New WhiteListCharacters()
        Pat.AddListFromString("a")

        Assert.IsTrue(Pat.IsValid("aaa"))

        Pat.AddListFromString(Nothing)
        Assert.IsTrue(Pat.IsValid("aaa"))
    End Sub

    <RowTest()> _
    <Row("bc", "", True)> _
    <Row("065", "", False)> _
    <Row(Nothing, "", False)> _
    <Row("0", "", False)> _
    <Row("a0545754", "", False)> _
    <Row("!asdfs", "", False)> _
    Public Sub PatternShortcutFromArrayAlpha(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim NumericPattern As IList(Of Char) = Patterns.EnglishAlphaMixedCase.ToCharArray()
        Assert.AreEqual(WhiteListCharacters.Valid(input, NumericPattern), expectedType, desc)
    End Sub

    <Test()> _
    Public Sub AddEmptyRules()
        Dim Tes As New RuleSet(Of String)
        Tes.AddRulesFromRuleSet(New RuleSet(Of String))
        Assert.AreEqual(Tes.Rules.Count, 0)

        Tes.AddRulesFromRuleSet(Nothing)
        Assert.AreEqual(Tes.Rules.Count, 0)

    End Sub


    <RowTest()> _
   <Row(1, 50, 10, "", True)> _
   <Row(-1, 1, 1, "", False)> _
   <Row(-1, 1, 0, "", True)> _
   <Row(0, 0, 0, "", False)> _
   <Row(0, 1, 0, "", False)> _
   <Row(-99999, 999999, 1, "", True)> _
    Public Sub WhitelistIntegersForRange(ByVal input As Integer, ByVal input2 As Integer, ByVal checkThis As Integer, ByVal desc As String, ByVal expectedType As Boolean)
        Dim X As New WhitelistIntegers()

        X.IntegerSets.Add(New WhitelistIntegers.IntegerRuleDefinition(input, WhitelistIntegers.IntegerRuleDefinition.Conditions.Bigger))
        X.IntegerSets.Add(New WhitelistIntegers.IntegerRuleDefinition(input2, WhitelistIntegers.IntegerRuleDefinition.Conditions.Smaller))

        Assert.AreEqual(X.IsValid(checkThis), expectedType, desc)

        'Same thing with add range
        Dim WRange As New WhitelistIntegers()
        WRange.AddRange(input, input2)
        Assert.AreEqual(WRange.IsValid(checkThis), expectedType, desc)

    End Sub

    <RowTest()> _
    <Row(1, 50, 10, "", True)> _
    <Row(-1, 1, 1, "", True)> _
    <Row(-1, 1, 0, "", True)> _
    <Row(0, 0, 0, "", True)> _
    <Row(0, 1, 0, "", True)> _
    <Row(-99999, 999999, 1, "", True)> _
    Public Sub WhitelistIntegersForRangeBoundaries(ByVal input As Integer, ByVal input2 As Integer, ByVal checkThis As Integer, ByVal desc As String, ByVal expectedType As Boolean)
        Dim WRange As New WhitelistIntegers()
        WRange.AddRangeIncludingBoundaries(input, input2)
        Assert.AreEqual(WRange.IsValid(checkThis), expectedType, desc)

    End Sub

    <RowTest()> _
    <Row(1, 2, "", False)> _
    <Row(-1, 0, "", False)> _
    <Row(0, 1, "", False)> _
    <Row(-1, -1, "", True)> _
    <Row(9, 9, "", True)> _
    Public Sub WhitelistIntegersForEqual(ByVal input As Integer, ByVal checkThis As Integer, ByVal desc As String, ByVal expectedType As Boolean)
        Dim X As New WhitelistIntegers()

        X.IntegerSets.Add(New WhitelistIntegers.IntegerRuleDefinition(input, WhitelistIntegers.IntegerRuleDefinition.Conditions.Equal))

        Assert.AreEqual(X.IsValid(checkThis), expectedType, desc)

        'Check with .Equal
        Dim WL As New WhitelistIntegers()
        WL.Equal(input)
        Assert.AreEqual(WL.IsValid(checkThis), expectedType, desc)


    End Sub


    <RowTest()> _
   <Row("www.google.com", "", True)> _
   <Row("http://www.google.com", "", False)> _
   <Row("www.goog_le.com", "", False)> _
   <Row("!google.com", "", False)> _
   <Row("googlecom", "", True)> _
   <Row("", "", False)> _
   <Row(Nothing, "", False)> _
   <Row("xxxxx.x.x..x.x.x", "", True)> _
   Public Sub HostNameRuleSet(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim Param As New Parameter(input, RuleSets.HostNameSet)
        Assert.AreEqual(Param.IsValid(), expectedType, desc)
    End Sub


End Class

